<?php namespace Admin\Controller;
use Hdphp\Controller\Controller;
Class GoodsController extends CommonController{
	private $model;
	public function __init(){
		parent::__init();
		$this->model=new \Common\Model\Goods;
	}
	
	public function index(){
		
		$gdata=$this->model->get();
//		p($gdata);exit;
		View::with('gdata',$gdata);
		
		
		View::make();
	}
	
	public function add(){
		if(IS_POST){
//			P($_POST);die;
			if($this->model->store()) View::success('添加成功',U('index'));
			View::error($this->model->getError());
		}
		$cdata=new \Common\Model\Cate;
		
		$cdata=$cdata->get();
		
		View::with('cdata',$cdata);
		
		$bdata=new \Common\Model\Brand;
		
		$bdata=$bdata->get();
		
		View::with('bdata',$bdata);
//		p($ndata);
//		p($cdata);
		View::make();
	}
	
	
	public function getTypeAttr(){
		$tid = Q('post.id',0,'intval');
		
		$data = Db::table('nature')->where("type_tid=$tid")->get();
		
		foreach ($data as $k => $v) {
			$data[$k]['nazhi'] = explode('|', $v['nazhi']);
		}
	
		echo json_encode($data);exit;
		
	}
	
	public function del(){
		$gsmodel=new \Common\Model\goodssx;
		$gxmodel=new \Common\Model\goodsxx;
		
		$gid=Q('get.gid',0,'intval');
		
		$this->model->where("gid={$gid}")->delete();
		$gxmodel->where("goods_gid={$gid}")->delete();
		$gsmodel->where("goods_gid={$gid}")->delete();
		View::success('删除成功',U('index'));
	}
//	编辑货品
	public function edit(){
		//获得要编辑的商品的主键
		$gid = Q('get.gid',0,'intval');
		if(IS_POST){
			
			if(!$this->model->edit()) View::error($this->model->getError());
			View::success('编辑成功',U('index',array('gid'=>"$gid")));
		}
		
		//根据gid获得type_typid从而获得该商品的类型属性值
		$tid = $this->model->where("gid={$gid}")->pluck('type_tid');
		//获得类型属性值（二维数组）
		$data = Db::table('nature')->where("type_tid={$tid}")->get();
		//定义商品类型和商品属性的两个空数组
		$spec=array();
		$attr=array();
		//循环二维数组将里面的属性值压入属性或者规格数组里，组成每个商品的属性和规格
	   foreach($data as $k => $v) {
	   		$attrvalue= explode('|', $v['nazhi']);
		   //如果是商品规格的时候
	   		if($v['natype']==1){
				$spec[]=$v;
	   		}else{
	   		//如果是商品属性的时候
	   			$attr[]=$v;
	   		}
        }
		//将规格和属性的值拆分成数组
		foreach ($spec as $k => $v) {
			$spec[$k]['nazhi'] = explode('|', $v['nazhi']);
		}
		foreach ($attr as $k => $v) {
			$attr[$k]['nazhi'] = explode('|', $v['nazhi']);
		}
		View::with('attr',$attr);
		View::with('spec',$spec);		
		//p($spec);	
		//获得商品表旧内容		
		$oldData = $this->model->where("gid={$gid}")->find();
		//关联商品属性表和类型属性表取出类型属性值和商品属性值用于编辑页面中属性和规格的循环
		$attr2 = Db::table('goodssx')->join('nature','nid','=','nature_nid')->where("goods_gid={$gid}")->get();
		foreach ($attr2 as $k => $v) {
			if(!empty($v['nazhi'])){
				$attr2[$k]['nazhi'] = explode('|', $v['nazhi']);
			}
		}
		//分配旧数据
		View::with('oldData',$oldData);
		View::with('attr2',$attr2);
		//获得商品品牌数据
		$brandModel = new \Common\Model\Brand;
		$brandData = $brandModel->get();
		
		//分配品牌数据
		View::with('brandData',$brandData);
		//实例化商品分类表
		$cateModel = new \Common\Model\Cate;
		$cateData = $cateModel->get();
		//变为树状结构
		$cateData = Data::tree($cateData,'cname','cid');
		View::with('cateData',$cateData);
		//获得商品详情表里的旧数据			
		$detail = Db::table('goodsxx')->where("goods_gid={$gid}")->get();
		foreach ($detail as $k => $v) {
			$detail[$k]['picture'] = explode(',', $v['picture']);
		}
		View::with('detail',$detail);
//		p($detail);
	    View::make();
	}
	
	//	上传图片
	public function upload(){
	    $file = Upload::path('Upload/Content/' . date('y/m'))->make();
	    if (empty($file)) {
	        $this->ajax(Upload::getError());
	    } else {
	        /** $file内部就是以下这个数组
	            $file = array(
	                0 => array(
		                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
		                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
		                'image' => 1
	            ),
	        );**/
	        //上传成功，把上传好的信息返给js
	        $data = $file[0];
	        echo json_encode($data);exit;
	    }
	}
	
	
	//uploadify异步上传(列表图)
//	public function list_picupload()
//	{
//	    $file = Upload::path('Upload/Content/' . date('y/m'))->make();
//	    if (empty($file)) {
//	        $this->ajax(Upload::getError());
//	    } else {
//	        /** $file内部就是以下这个数组
//	            $file = array(
//	                0 => array(
//		                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
//		                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
//		                'image' => 1
//	            ),
//	        );**/
//	        //上传成功，把上传好的信息返给js
//	        $data = $file[0];
//	        echo json_encode($data);exit;
//	    }
//	}
//	
//	
//	//uploadify异步上传(商品图册)
//	public function photoupload()
//	{
//	    $file = Upload::path('Upload/Content/' . date('y/m'))->make();
//	    if (empty($file)) {
//	        $this->ajax(Upload::getError());
//	    } else {
//	        /** $file内部就是以下这个数组
//	            $file = array(
//	                0 => array(
//		                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
//		                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
//		                'image' => 1
//	            ),
//	        );**/
//	        //上传成功，把上传好的信息返给js
//	        $data = $file[0];
//	        echo json_encode($data);exit;
//	    }
//	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
?>